/*
 * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef FOUNDATION_ACE_FRAMEWORKS_CORE_RECORDER_EVENT_CONFIG_H
#define FOUNDATION_ACE_FRAMEWORKS_CORE_RECORDER_EVENT_CONFIG_H

#include <string>
#include <list>
#include <memory>
#include <set>

#include "base/json/json_util.h"

namespace OHOS::Ace::Recorder {
enum EventCategory : int32_t {
    CATEGORY_PAGE = 0,
    CATEGORY_COMPONENT,
    CATEGORY_EXPOSURE,
};

struct ExposureCfg {
    std::string id;
    float ratio;
    int duration;

    bool operator <(const ExposureCfg& ec) const
    {
        return id < ec.id;
    }
};

struct PageCfg {
    std::list<std::string> shareNodes;
    std::list<ExposureCfg> exposureCfgs;
};

using Config = std::unordered_map<std::string, PageCfg>;
using Switch = std::unordered_map<EventCategory, bool>;

class EventConfig final {
public:
    EventConfig();
    ~EventConfig() = default;

    void Init(std::string& config);
    bool IsEnable();
    bool IsCategoryEnable(EventCategory category);
    std::shared_ptr<Config> GetConfig();

private:
    inline void ParseSwitch(std::unique_ptr<JsonValue>& jsonObj);
    inline void ParseShareNode(std::unique_ptr<JsonValue>& shareNodeArray, PageCfg& pageCfg);
    inline void ParseExposureCfg(std::unique_ptr<JsonValue>& exposureCfgArray, PageCfg& pageCfg);
    bool enable_ = false;
    std::shared_ptr<Switch> switches_;

    std::shared_ptr<Config> config_;
};
} // namespace OHOS::Ace::Recorder
#endif // FOUNDATION_ACE_FRAMEWORKS_CORE_RECORDER_EVENT_CONFIG_H